Make sure to fork again after setsid() so that child cannot regain CTTY.
authorAnthony Liguori <anthony@codemonkey.ws>
Mon, 12 Dec 2005 15:11:13 +0000 (15:11 +0000)
committerAnthony Liguori <anthony@codemonkey.ws>
Mon, 12 Dec 2005 15:11:13 +0000 (15:11 +0000)
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
tools/console/daemon/utils.c
tools/xenstore/xenstored_core.c

index 372f19e3e5d38255453f49a64f8b7a95e1cfe585..524e8068dc8cb894f53e8b647d12f86a8282eacb 100644 (file)
@@ -90,6 +90,12 @@ void daemonize(const char *pidfile)
 
        setsid();
 
+       if ((pid = fork()) > 0) {
+               exit(0);
+       } else if (pid == -1) {
+               err(errno, "fork() failed");
+       }
+
        /* redirect fd 0,1,2 to /dev/null */
        if ((fd = open("/dev/null",O_RDWR)) == -1) {
                exit(1);
index a259f3a41f77c0613efbb068ad71b46bf4a4f3e6..6afde0ae80710a82eae4854f277c9a0496ce3d5f 100644 (file)
@@ -1491,6 +1491,13 @@ static void daemonize(void)
 
        /* Session leader so ^C doesn't whack us. */
        setsid();
+
+       /* Let session leader exit so child cannot regain CTTY */
+       if ((pid = fork()) < 0)
+               barf_perror("Failed to fork daemon");
+       if (pid != 0)
+               exit(0);
+
 #ifndef TESTING        /* Relative paths for socket names */
        /* Move off any mount points we might be in. */
        chdir("/");